<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            width: 400px;
            border-bottom: 1px solid gray;
            margin: 100px auto;
            position: relative;
        }
        .box input{
            width: 370px;
            height: 30px;
            border: 0;
            outline: none;
            /* outline: 轮廓，是边框边缘的外围*/
        }
        .box img{
            position: absolute;
            top: 2px;
            right: 2px;
            width: 20px;
        }
    </style>
</head>

<body>
    <div class="box">
        <label for="pas">密码：</label>
        <input type="password" id="pas">
        <img src="images/close.png" alt="隐藏" id="eye">
    </div>
    <script>
        //1.获取元素
        var pas = document.getElementById('pas')
        var eye = document.getElementById('eye')
        //2.注册事件 处理程序
        var flag = 0;
        eye.onclick = function(){
            if(flag == 0){
                pas.type = "text";
                eye.src = 'images/open.png';
                flag = 1;
            }else{
                pas.type = 'password';
                eye.src = 'images/close.png'
                flag = 0;
            }
          
        }
    </script>
</body>

</html>